<?php

/*
 * Retreiving info about current (non closed) SRs.
 * 
 */
$query = ' 
select  tse.team team,
        tse.tse_name tse_name, 
        srsubstatus.val srsubstatus, 
        srs.priority priority,
        srs.sr sr,
        FROM_UNIXTIME(srs.opened) opened,
        FROM_UNIXTIME(srs.modified) modified,
        FROM_UNIXTIME(srs.last_public) lastpublic,
        srs.escalation_flag escalated,
        product.val product,
        custregion.val custregion,
        srs.production_down pdown
from srs, tse, srsubstatus, custregion, product 
where 
    srs.status<>4
    and tse.owner_id=srs.owner 
    and srsubstatus.id=srs.sub_status 
    and tse.owner_id in (' . $ids . ')    
    and product.id=srs.product     
    and custregion.id=srs.end_user_support_group 
order by tse.team, tse.tse_name, srsubstatus.val, srs.priority, srs.sr;';

/*
 * <td width=60>Days w/o update</td>    
 * <td width=60>T3 Owner</td>           JOIN 
 */

@ $result = mysql_query($query);
while (@ $res = mysql_fetch_assoc($result)) {
    $current[$res['team']][$res['tse_name']][$res['srsubstatus']][$res['priority']][$res['sr']]['opened'] = $res['opened'];
    $current[$res['team']][$res['tse_name']][$res['srsubstatus']][$res['priority']][$res['sr']]['modified'] = $res['modified'];
    $current[$res['team']][$res['tse_name']][$res['srsubstatus']][$res['priority']][$res['sr']]['lastpublic'] = $res['lastpublic'];
    $current[$res['team']][$res['tse_name']][$res['srsubstatus']][$res['priority']][$res['sr']]['product'] = $res['product'];
    $current[$res['team']][$res['tse_name']][$res['srsubstatus']][$res['priority']][$res['sr']]['escalated'] = $res['escalated'];
    $current[$res['team']][$res['tse_name']][$res['srsubstatus']][$res['priority']][$res['sr']]['custregion'] = $res['custregion'];
    $current[$res['team']][$res['tse_name']][$res['srsubstatus']][$res['priority']][$res['sr']]['pdown'] = $res['pdown'];    
}
@ mysql_free_result($result);

/*
 * Retreiving counters  for building table.
 * 
 */

$query = 'select tse.team team, tse.tse_name tse_name, count(srs.sr) srs_t 
from srs, tse 
where srs.status<>4 
and tse.owner_id=srs.owner 
and tse.owner_id in (' . $ids . ') 
group by tse.team, tse.tse_name
order by tse.team, tse.tse_name;';
@ $result = mysql_query($query);
while (@ $res = mysql_fetch_assoc($result))
    $total[$res['team']][$res['tse_name']]['srs_t'] = $res['srs_t'];
@ mysql_free_result($result);

$query = 'select tse.team team, 
       tse.tse_name tse_name,
       srsubstatus.val srsubstatus, 
       count(srsubstatus.val) substatus_t
from srs, tse, srsubstatus 
where srs.status<>4 
  and tse.owner_id=srs.owner 
  and srsubstatus.id=srs.sub_status 
  and tse.owner_id in (' . $ids . ') 
group by tse.team, tse.tse_name, srsubstatus.val
order by tse.team, tse.tse_name, srsubstatus.val;';
@ $result = mysql_query($query);
while (@ $res = mysql_fetch_assoc($result))
    $total[$res['team']][$res['tse_name']][$res['srsubstatus']]['substatus_t'] = $res['substatus_t'];
@ mysql_free_result($result);


$query = 'select tse.team team, 
       tse.tse_name tse_name, 
       srsubstatus.val srsubstatus, 
       srs.priority priority, 
       count(srs.priority) priority_t
from srs, tse, srsubstatus 
where srs.status<>4 
  and tse.owner_id=srs.owner 
  and srsubstatus.id=srs.sub_status 
  and tse.owner_id in (' . $ids . ') 
group by tse.team, tse.tse_name, srsubstatus.val, srs.priority
order by tse.team, tse.tse_name, srsubstatus.val, srs.priority;';

@ $result = mysql_query($query);

while (@ $res = mysql_fetch_assoc($result))
    $total[$res['team']][$res['tse_name']][$res['srsubstatus']][$res['priority']]['priority_t'] = $res['priority_t'];
@ mysql_free_result($result);

//echo $query .'<br>';
//echo 'Current SR\'s Report: <pre>';print_r($current);echo'</pre>';
?>
